module majiang.Views {
    /**
     * ScoreBoard
     * 记分板
     */
    export class ScoreBoard extends eui.Component{
        //eui
        public timeBit:eui.BitmapLabel;
        public paixingGroup:eui.Group;

        //data
        public timer:egret.Timer;
        public paixingArr:Array<majiang.Views.HupaiPaixing> = [];
        public hupaiStrArr:Array<string>;

        constructor() {
            super();
            this.touchChildren = false;
            this.touchEnabled = false;
            this.initScoreBoard();
        }


        /**
         * initScoreBoard
         * 注册计时器
         */
        public initScoreBoard() {
            this.timer = new egret.Timer(1000);
            this.timer.addEventListener(egret.TimerEvent.TIMER,this.timerFunc,this);
        }

        private timerFunc() {
            if(majiang.Datas.Playing.timeSec < 1){
                // time over
                majiang.Controllers.Play.gameOver();
                return
            }

            majiang.Datas.Playing.timeSec --;
            egret.Tween.get(this.timeBit).to({scaleX:1.2,scaleY:1.2},300).to({scaleX:1,scaleY:1},300).call(this.updateTimeBit,this);
        }

        //更新时间文字
        private updateTimeBit() {
            this.timeBit.text = majiang.Datas.Playing.timeSec + "";
        }
        /**
         * gameStart
         * 游戏开始的数据初始化
         */
        public gameStart() {
            majiang.Datas.Playing.timeSec = majiang.Datas.Playing.timeMax;
            this.updateTimeBit();
            this.timerStart();
        }

        /**
         * stopTime
         */
        public timerStop() {      
            this.timer.stop();
        }

        /**
         * startTime
         */
        public timerStart() {
            this.timer.start();
        }

        public hupai(hupaiArr:Array<string>){
            this.hupaiStrArr = hupaiArr.slice(0);
            this.addAllPaixing();
        }
        /**
         * addPaixing
         */
        private addAllPaixing() {
            if(this.hupaiStrArr.length < 1){
                //胡牌后 显示的时间
                egret.Tween.get(this).wait(2000).call(()=>{
                    this.removeAllPaixing();
                },this);
                return
            }

            egret.Tween.get(this).wait(200).call(()=>{
                this.paixingGroup.addChild(this.getPaixing(this.hupaiStrArr.shift()));
                this.addAllPaixing();
            },this)
        }

        private getPaixing(paixing:string) {
            if(this.paixingArr.length < 1){
                this.paixingArr.push(new majiang.Views.HupaiPaixing());
            }

            var pai = <majiang.Views.HupaiPaixing> this.paixingArr.shift();
            pai.paixingText = paixing;
            return pai;
        }

        public removeAllPaixing() {
            for(var i=0; i < this.paixingGroup.numChildren; i ++){
                var element = <majiang.Views.HupaiPaixing> this.paixingGroup.getChildAt(i);
                this.paixingArr.push(element);
            }
            this.paixingGroup.removeChildren();
        }
    }
}